# -*- coding: utf-8 -*-

# PROTOTYPE, NOT TEST-COVERED!

class editor_logic_state_editing_query :
    def __init__ ( self , mediator ) :
        self . _mediator = mediator
        self . _awake = False
        self . _kb_types_count = 0
        self . _first_kb_type = None
        self . _greater_common_kb_types_part = None
    def activate_editor_state_editing_query ( self ) :
        self . _awake = True
    def hide_editor ( self ) :
        self . _awake = False
    def new_kb_type_added ( self , kb_type ) :
        self . _kb_types_count += 1
        if self . _kb_types_count == 1 :
            self . _first_kb_type = kb_type
    def receive_is_any_marks_left_in_editor_contents ( self , any_marks ) :
        if self . _awake :
            if not any_marks :
                self . _awake = False
                self . _mediator . activate_editor_state_creating_new_query ( )
    def set_greater_common_kb_types_part ( self , part ) :
        self . _greater_common_kb_types_part = part
    def on_key_ctrl_f5 ( self ) :
        if self . _awake :
            self . _mediator . add_mark_to_editor_contents ( )
            self . _mediator . add_text_to_editor_contents ( ' !§' )
            self . _mediator . add_entry_to_editor_contents ( )
            self . _mediator . set_editor_status_string ( 'type new entry name' )
            self . _awake = False
            self . _mediator . activate_editor_state_typing_new_kb_type ( )
    def on_key_f5 ( self ) :
        if self . _awake :
            if self . _kb_types_count == 0 :
                self . _mediator . set_editor_status_string ( 'no entries to select from' )
            elif self . _kb_types_count == 1 :
                self . _mediator . add_mark_to_editor_contents ( )
                self . _mediator . add_text_to_editor_contents ( ' §' + self . _first_kb_type )
                self . _mediator . set_editor_status_string ( 'existing entry created' )
                self . _awake = False
                self . _mediator . activate_editor_state_editing_query ( )
            else :
                self . _mediator . add_mark_to_editor_contents ( )
                self . _mediator . add_text_to_editor_contents ( ' §' )
                self . _mediator . add_entry_to_editor_contents ( )
                self . _mediator . set_editor_content_entry_string ( self . _greater_common_kb_types_part )
                self . _mediator . set_editor_status_string ( 'type existing entry name' )
                self . _awake = False
                self . _mediator . activate_editor_state_typing_existing_kb_type ( )
    def on_key_return ( self ) :
        if self . _awake :
            self . _mediator . remove_all_marks_from_editor_contents ( )
            self . _mediator . add_text_to_editor_contents ( ';\n' )
            self . _mediator . set_editor_status_string ( 'query finished' )
            self . _awake = False
            self . _mediator . activate_editor_state_creating_new_query ( )
    def on_key_backspace ( self ) :
        if self . _awake :
            self . _mediator . set_editor_status_string ( 'last entry deleted' )
            self . _mediator . delete_text_from_last_mark_to_end_of_editor_contents ( )
            self . _mediator . request_is_any_marks_left_in_editor_contents ( )
